package Product;

import java.io.File;
import java.sql.PreparedStatement;
import java.text.DecimalFormat;
import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class ProductMyba {

	private SqlSession session;
	ProductVo productvo;
	DecimalFormat df = new DecimalFormat("###,###,###");
	int fileSize = 10 * 1024 * 1024;
	String fileEncoding = "utf-8";
	int listSize = 10;// 한페이지에 보여질 행수
	int blockSize = 10;// 보여질 페이지 이동 번호수
	int totSize = 0;// 검색될 데이터 총갯수
	int totPage = 0;// 전체페이지수
	int totBlock = 0;// 전체블럭
	int nowPage = 1;// 현재페이지
	int nowBlock = 1;// 현재블럭
	int startNo = 1;// 목록의 시작 위치
	int endNo = 3;// 목록의 끝 위치
	int startPage = 1;// 현재 블럭의 시작 페이지 번호
	int endPage = 2;// 현재 블럭의 끝페이지번호
	String serial = "";

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	public SqlSession getSession() {
		return session;
	}

	public void setSession(SqlSession session) {
		this.session = session;
	}

	public void computePage() {// 페이징과 관계된 연산
		totPage = (int) Math.ceil(totSize * 1.0 / listSize);
		totBlock = (int) Math.ceil(totPage * 1.0 / blockSize);
		nowBlock = (int) Math.ceil(nowPage * 1.0 / blockSize);

		endPage = nowBlock * blockSize;
		startPage = endPage - blockSize + 1;
		if (totPage < endPage)
			endPage = totPage;
		endNo = nowPage * listSize;
		startNo = endNo - listSize + 1;
		if (totSize < endNo)
			endNo = totSize;
	}

	public ProductMyba() {
		try {
			session = ProductFactory.getFactory().openSession();
			// MemberFactory에서 가져다가 사용해도 되는데 굳이 만들필요가읍음

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public ArrayList<ProductVo> list() {
		nowPage = productvo.getNowPage();
		ArrayList<ProductVo> data = null;
		try {
			int cnt = (int) session.selectOne("product.htotsize", productvo);
			totSize = cnt;
			computePage();
			productvo.setStartNo(startNo);
			productvo.setEndNo(endNo);
			data = (ArrayList<ProductVo>) session.selectList("product.list",
					productvo);

			session.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return data;
	}

	public ArrayList<ProductVo> list2() {

		nowPage = productvo.getNowPage();

		ArrayList<ProductVo> data = null;
		ArrayList<ProductVo> al = null;
		try {
			int cnt = (int) session.selectOne("product.totsize", productvo);
			totSize = cnt;

			computePage();

			productvo.setStartNo(startNo);
			productvo.setEndNo(endNo);

			data = (ArrayList<ProductVo>) session.selectList("product.ilist",
					productvo);
			for (int i = 0; i < data.size(); i++) {
				data.get(i).setPD_max(
						df.format(Integer.parseInt(data.get(i).getPD_max()
								.replace(",", "").replace("만원", "")))
								+ "만원");
			}
			session.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return data;
	}

	public boolean insert() {

		boolean flag = false;

		String realPath = productvo.getRequest().getServletContext()
				.getRealPath("WebContent");

		String path = realPath.replace("\\WebContent", "");
		String pro = path.substring(path.lastIndexOf("\\") + 1, path.length());
		path = realPath.substring(0, realPath.indexOf(".")) + pro
				+ "\\WebContent\\intranet\\product\\photo\\";

		try {
			File file = new File(path);
			if (!file.exists()) {
				file.mkdir();
			}
			MultipartRequest mr = new MultipartRequest(productvo.getRequest(),
					path, fileSize, fileEncoding, new DefaultFileRenamePolicy());
			productvo.setPD_no(serial);
			productvo.setPD_prd(mr.getParameter("PD_prd"));
			productvo.setPD_max(mr.getParameter("PD_max"));
			productvo.setPD_min((mr.getParameter("PD_min")));
			productvo.setPD_nm(mr.getParameter("PD_nm"));
			productvo.setPD_rate((mr.getParameter("PD_rate")));
			productvo.setPD_fnm(mr.getFilesystemName("PD_fnm"));
			productvo.setPD_pback(mr.getParameter("PD_pback"));
			productvo.setPD_content(mr.getParameter("PD_content"));

			int cnt = session.insert("product.insert", productvo);
			if (cnt > 0) {
				flag = true;
				session.commit();
			}
			session.close();

		} catch (Exception ex) {
			ex.getStackTrace();
		}

		return flag;
	}

	public ProductVo view() {
		ProductVo vo = null;
		try {
			vo = (ProductVo) session.selectOne("product.view", productvo);
			session.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return vo;
	}

	public boolean update() {
		boolean flag = false;
		try {
			int cnt = session.update("product.update", productvo);
			if (cnt > 0) {
				flag = true;
				session.commit();
			}
			session.close();
		} catch (Exception ev) {
			ev.printStackTrace();
		}
		return flag;
	}

	public ProductVo getProductvo() {
		return productvo;
	}

	public void setProductvo(ProductVo productvo) {
		this.productvo = productvo;
	}

	public int getFileSize() {
		return fileSize;
	}

	public void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}

	public String getFileEncoding() {
		return fileEncoding;
	}

	public void setFileEncoding(String fileEncoding) {
		this.fileEncoding = fileEncoding;
	}

	public int getListSize() {
		return listSize;
	}

	public void setListSize(int listSize) {
		this.listSize = listSize;
	}

	public int getBlockSize() {
		return blockSize;
	}

	public void setBlockSize(int blockSize) {
		this.blockSize = blockSize;
	}

	public int getTotSize() {
		return totSize;
	}

	public void setTotSize(int totSize) {
		this.totSize = totSize;
	}

	public int getTotPage() {
		return totPage;
	}

	public void setTotPage(int totPage) {
		this.totPage = totPage;
	}

	public int getTotBlock() {
		return totBlock;
	}

	public void setTotBlock(int totBlock) {
		this.totBlock = totBlock;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;

	}

	public int getNowBlock() {
		return nowBlock;
	}

	public void setNowBlock(int nowBlock) {
		this.nowBlock = nowBlock;
	}

	public int getStartNo() {
		return startNo;
	}

	public void setStartNo(int startNo) {
		this.startNo = startNo;
	}

	public int getEndNo() {
		return endNo;
	}

	public void setEndNo(int endNo) {
		this.endNo = endNo;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

	public void setProductVo(ProductVo vo) {
		this.productvo = vo;
	}

}
